<?php

set_time_limit(1000);

require dirname(__FILE__).'/../config.php';

$root_dir = '../';

include WP_VERSION_PATH . '/header.php';

echo '<h2>WP Version Upgrader</h2>'."\n\n";

if( (false === USE_SQLITE) && (false === USE_MYSQL) )
{
	if( true === wp_version_install_serialized())
		echo '<p><strong><a href="index.php">Done!</a></strong></p>';
	else
		echo '<p>Failed!</p>';
}
else if( (true === USE_SQLITE) && (false === USE_MYSQL) )
{
	if( true === wp_version_upgrade_sqlite())
		echo '<p><strong><a href="index.php">Done!</a></strong></p>';
	else
		echo '<p>Failed!</p>';
}

include WP_VERSION_PATH . '/footer.php';

function wp_version_upgrade_sqlite()
{
	$pdo = new PDO('sqlite:'.SQLITE_PATH);
	$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

	try
	{
		$allTypes = $pdo->query("SELECT DISTINCT * FROM wp_version_info")->fetchAll(PDO::FETCH_OBJ);
		$namePrepare = $pdo->prepare("INSERT INTO wp_types (info_type, info_name) VALUES (?, ?);");
		$defPrepare = $pdo->prepare("INSERT INTO wp_types_definition (info_type, info_name, version, definition) VALUES (?, ?, ?, ?);");
	}
	catch(PDOException $e)
	{
		echo $e->getMessage();
		return false;
	}

	$save = array();
	
	$prepare1_0_0 = $pdo->prepare("UPDATE wp_types SET version_1_0_0 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare1_0_1 = $pdo->prepare("UPDATE wp_types SET version_1_0_1 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare1_0_2 = $pdo->prepare("UPDATE wp_types SET version_1_0_2 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare1_2_0 = $pdo->prepare("UPDATE wp_types SET version_1_2_0 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare1_2_1 = $pdo->prepare("UPDATE wp_types SET version_1_2_1 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare1_2_2 = $pdo->prepare("UPDATE wp_types SET version_1_2_2 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare1_5_0 = $pdo->prepare("UPDATE wp_types SET version_1_5_0 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare1_5_1 = $pdo->prepare("UPDATE wp_types SET version_1_5_1 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare1_5_2 = $pdo->prepare("UPDATE wp_types SET version_1_5_2 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_0_0 = $pdo->prepare("UPDATE wp_types SET version_2_0_0 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_0_1 = $pdo->prepare("UPDATE wp_types SET version_2_0_1 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_0_4 = $pdo->prepare("UPDATE wp_types SET version_2_0_4 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_0_5 = $pdo->prepare("UPDATE wp_types SET version_2_0_5 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_0_6 = $pdo->prepare("UPDATE wp_types SET version_2_0_6 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_0_7 = $pdo->prepare("UPDATE wp_types SET version_2_0_7 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_0_8 = $pdo->prepare("UPDATE wp_types SET version_2_0_8 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_0_9 = $pdo->prepare("UPDATE wp_types SET version_2_0_9 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_0_10 = $pdo->prepare("UPDATE wp_types SET version_2_0_10 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_0_11 = $pdo->prepare("UPDATE wp_types SET version_2_0_11 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_1_0 = $pdo->prepare("UPDATE wp_types SET version_2_1_0 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_1_1 = $pdo->prepare("UPDATE wp_types SET version_2_1_1 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_1_2 = $pdo->prepare("UPDATE wp_types SET version_2_1_2 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_1_3 = $pdo->prepare("UPDATE wp_types SET version_2_1_3 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_2_0 = $pdo->prepare("UPDATE wp_types SET version_2_2_0 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_2_1 = $pdo->prepare("UPDATE wp_types SET version_2_2_1 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_2_2 = $pdo->prepare("UPDATE wp_types SET version_2_2_2 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_2_3 = $pdo->prepare("UPDATE wp_types SET version_2_2_3 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_3_0 = $pdo->prepare("UPDATE wp_types SET version_2_3_0 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_3_1 = $pdo->prepare("UPDATE wp_types SET version_2_3_1 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_3_2 = $pdo->prepare("UPDATE wp_types SET version_2_3_2 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_3_3 = $pdo->prepare("UPDATE wp_types SET version_2_3_3 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_5_0 = $pdo->prepare("UPDATE wp_types SET version_2_5_0 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_5_1 = $pdo->prepare("UPDATE wp_types SET version_2_5_1 = 1 WHERE info_type = ? AND info_name = ?");
	$prepare2_6_0 = $pdo->prepare("UPDATE wp_types SET version_2_6_0 = 1 WHERE info_type = ? AND info_name = ?");

	foreach($allTypes as $row)
	{

		try
		{
			$namePrepare->execute(array($row->info_type, $row->info_name));
		}
		catch(PDOException $e) { }
		
		try
		{
			$defPrepare->execute(array($row->info_type, $row->info_name, $row->info_version, $row->info_definition));
		}
		catch(PDOException $e) { }

		switch($row->info_version)
		{
			case '0.71':
				$pdo->query("UPDATE wp_types SET version_0_71 = 1 WHERE info_type = '".$row->info_type."' AND info_name = '".$row->info_name."'");
				break;
			case '1.0.0':
				$prepare1_0_0->execute(array($row->info_type, $row->info_name));
				break;
			case '1.0.1':
				$prepare1_0_1->execute(array($row->info_type, $row->info_name));
				break;
			case '1.0.2':
				$prepare1_0_2->execute(array($row->info_type, $row->info_name));
				break;
			case '1.2.0':
				$prepare1_2_0->execute(array($row->info_type, $row->info_name));
				break;
			case '1.2.1':
				$prepare1_2_1->execute(array($row->info_type, $row->info_name));
				break;
			case '1.2.2':
				$prepare1_2_2->execute(array($row->info_type, $row->info_name));
				break;
			case '1.5.0':
				$prepare1_5_0->execute(array($row->info_type, $row->info_name));
				break;
			case '1.5.1':
				$prepare1_5_1->execute(array($row->info_type, $row->info_name));
				break;
			case '1.5.2':
				$prepare1_5_2->execute(array($row->info_type, $row->info_name));
				break;
			case '2.0.0':
				$prepare2_0_0->execute(array($row->info_type, $row->info_name));
				break;
			case '2.0.1':
				$prepare2_0_1->execute(array($row->info_type, $row->info_name));
				break;
			case '2.0.4':
				$prepare2_0_4->execute(array($row->info_type, $row->info_name));
				break;
			case '2.0.5':
				$prepare2_0_5->execute(array($row->info_type, $row->info_name));
				break;
			case '2.0.6':
				$prepare2_0_6->execute(array($row->info_type, $row->info_name));
				break;
			case '2.0.7':
				$prepare2_0_7->execute(array($row->info_type, $row->info_name));
				break;
			case '2.0.8':
				$prepare2_0_8->execute(array($row->info_type, $row->info_name));
				break;
			case '2.0.9':
				$prepare2_0_9->execute(array($row->info_type, $row->info_name));
				break;
			case '2.0.10':
				$prepare2_0_10->execute(array($row->info_type, $row->info_name));
				break;
			case '2.0.11':
				$prepare2_0_11->execute(array($row->info_type, $row->info_name));
				break;
			case '2.1.0':
				$prepare2_1_0->execute(array($row->info_type, $row->info_name));
				break;
			case '2.1.1':
				$prepare2_1_1->execute(array($row->info_type, $row->info_name));
				break;
			case '2.1.2':
				$prepare2_1_2->execute(array($row->info_type, $row->info_name));
				break;
			case '2.1.3':
				$prepare2_1_3->execute(array($row->info_type, $row->info_name));
				break;
			case '2.2.0':
				$prepare2_2_0->execute(array($row->info_type, $row->info_name));
				break;
			case '2.2.1':
				$prepare2_2_1->execute(array($row->info_type, $row->info_name));
				break;
			case '2.2.2':
				$prepare2_2_2->execute(array($row->info_type, $row->info_name));
				break;
			case '2.2.3':
				$prepare2_2_3->execute(array($row->info_type, $row->info_name));
				break;
			case '2.3.0':
				$prepare2_3_0->execute(array($row->info_type, $row->info_name));
				break;
			case '2.3.1':
				$prepare2_3_1->execute(array($row->info_type, $row->info_name));
				break;
			case '2.3.2':
				$prepare2_3_2->execute(array($row->info_type, $row->info_name));
				break;
			case '2.3.3':
				$prepare2_3_3->execute(array($row->info_type, $row->info_name));
				break;
			case '2.5.0':
				$prepare2_5_0->execute(array($row->info_type, $row->info_name));
				break;
			case '2.5.1':
				$prepare2_5_1->execute(array($row->info_type, $row->info_name));
				break;
			case '2.6.0':
				$prepare2_6_0->execute(array($row->info_type, $row->info_name));
				break;
		}
		
		
	}

	$pdo->query("DELETE FROM wp_version_info");

	return true;
}